package dao.entity;

import dao.MysqlConfiguration;
import dao.interf.UserDao;
import dao.util.DaoTools;
import tools.TimeProcess;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomerDaoImpl implements UserDao {
    public static int cidConf=10000;

    public boolean userLogin(String cname,String pwd) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select * from customers where cname=? and pwd=?";
        ResultSet rs=null;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,cname);
            pst.setString(2,pwd);
            rs=pst.executeQuery();
        } catch (SQLException e) {
//            throw new RuntimeException(e);
            System.out.println("创建select顾客语句对象失败:"+e.getMessage());
        }
        boolean tag=false;
        try {
            if (rs != null) {
                tag=rs.next();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(tag){
            //登录成功，要及时更新登录状态和登录时间
            LocalDateTime ldt= LocalDateTime.now();//创建一个当前时间对象
            DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//创建一个日期格式化对象
            String lg_time=dtf.format(ldt);//当前时间
            String sql_update="update customers set lg_time=?,c_status=1 where cname=?";
            try {
                PreparedStatement pst_update=connection.prepareStatement(sql_update);
                pst_update.setString(1,lg_time);
                pst_update.setString(2,cname);
                pst_update.executeUpdate();
            } catch (SQLException e) {
//                throw new RuntimeException(e);
                System.out.println("创建update customers顾客语句对象失败:"+e.getMessage());
            }
        }
        return tag;
    }

    public int addCustomer(String cname,String pwd,String c_phone) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="insert into customers values(?,?,?,?,?,?,?,?)";
        System.out.println(sql);
        int n=0;//受影响的行数
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            System.out.println("创建insert顾客语句对象成功!");
            cidConf+= DaoTools.countPersonSum("customers") +1;
            pst.setString(1,cidConf+"");
            pst.setString(2,cname);
            pst.setString(3,pwd);
            pst.setString(4,null);
            pst.setString(5,null);
            pst.setString(6,c_phone);
            String lg_time= TimeProcess.getCurrentTime();//当前时间
            pst.setString(7,lg_time);
            pst.setInt(8,1);
            n=pst.executeUpdate();
        } catch (SQLException e) {
            System.out.println("创建insert顾客语句对象失败:"+e.getMessage());
        }
        return n;
    }

    @Override
    public ResultSet selectUserByName(String cname) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select * from customers where cname=?";
        System.out.println(sql);
        ResultSet rs=null;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,cname);
            rs= pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select顾客语句对象失败:"+e.getMessage());
        }
        return rs;
    }

    @Override
    public boolean isPhoneExisting(String phone) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select * from customers where c_phone=?";
        System.out.println(sql);
        ResultSet rs=null;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,phone);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select顾客语句对象失败:"+e.getMessage());
        }
        boolean tag=false;
        try {
            if (rs != null) {
                tag=rs.next();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return tag;
    }

    @Override
    public boolean isOnline(String cname) {
        Connection connection=MysqlConfiguration.MysqlConnection();
        String sql="select c_status from customers where cname=?";
        ResultSet rs=null;
        boolean tag=false;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,cname);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            System.out.println("创建select c_status顾客语句对象失败:"+e.getMessage());
        }
        try {
            rs.next();
            tag=rs.getBoolean("c_status");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return tag;
    }

    @Override
    public int logOut(String cname) {
        Connection connection= MysqlConfiguration.MysqlConnection();
        String sql="update customers set c_status=0 where cname=?";
        int n=0;
        try {
            PreparedStatement pst=connection.prepareStatement(sql);
            pst.setString(1,cname);
            n=pst.executeUpdate();
        } catch (SQLException e) {
//            throw new RuntimeException(e);
            System.out.println("创建update customers顾客语句对象失败:"+e.getMessage());
        }
        return n;
    }
}
